<template>
  <div>
    <el-card class="box-card" style="width: 300px">
      <div slot="header" class="clearfix">
        <span>{{hr.name}}</span>
      </div>
      <div>
        <div style="display: flex;justify-content: center;margin-bottom: 10px">
          <el-upload
              action="/system/hr/updateUserFace"
              :show-file-list="false"
              :on-success="onSuccess"
              :data="hr"
              >
            <img :src="hr.userFace" title="点击头像可更换图片" style="width: 100px;height: 100px;border-radius: 50%"/>
          </el-upload>

        </div>
        <div>
          <span>电话号码：<el-tag type="primary" size="small">{{hr.telephone}}</el-tag></span>
        </div>
        <div>
          <span>手机号码：<el-tag type="primary" size="small">{{hr.phone}}</el-tag></span>
        </div>
       <div>
         <span>居住地址：<el-tag type="primary" size="small">{{hr.address}}</el-tag></span>
       </div>
        <div>
          <span>用户标签：<el-tag type="primary" v-for="(role,index) in hr.roles" :key="index">{{role.nameZh}}</el-tag></span>
        </div>

        <div style="display: flex;justify-content: space-around;margin-top: 10px">
          <el-button type="primary" @click="showUpdateUserInfoView">修改信息</el-button>
          <el-button type="danger" @click="showUpdatePwdView">修改密码</el-button>
        </div>
      </div>
    </el-card>

    <el-dialog
        title="修改信息"
        :visible.sync="editHrInfoDialogVisible"
        width="30%">
      <div>
        <el-form :model="hrEdit" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户昵称" prop="name">
            <el-input v-model="hrEdit.name"></el-input>
          </el-form-item>
          <el-form-item label="电话号码" prop="telephone">
            <el-input v-model="hrEdit.telephone"></el-input>
          </el-form-item>
          <el-form-item label="手机号码" prop="phone">
            <el-input v-model="hrEdit.phone"></el-input>
          </el-form-item>
          <el-form-item label="居住地址" prop="address">
            <el-input type="textarea" v-model="hrEdit.address"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelDialog">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
        title="修改密码"
        :visible.sync="editPwdDialogVisible"
        width="30%">
        <div>
          <el-form :model="pwdForm" status-icon :rules="pwdFormRules" ref="pwdForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="旧密码" prop="oldPass">
              <el-input type="password" v-model="pwdForm.oldPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPass">
              <el-input type="password" v-model="pwdForm.newPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="新密码确认" prop="checkPass">
              <el-input type="password" v-model="pwdForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
        <el-button @click="resetFormOfUpdatePwd('pwdForm')">取 消</el-button>
        <el-button type="primary" @click="submitFormOfUpdatePwd('pwdForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "UserInfo",
  data(){
    let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.pwdForm.checkPass !== '') {
          this.$refs.pwdForm.validateField('checkPass');
        }
        callback();
      }
    };
    let validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.pwdForm.newPass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return{
      hr: {

      },
      hrEdit:{

      },
      editHrInfoDialogVisible:false,
      rules: {
        name: [
          {required: true, message: '请输入昵称', trigger: 'blur'},
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        telephone: [
          {required: true, message: '请输入电话号码', trigger: 'blur'},
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
        ],
        address: [
          {required: true, message: '请输入居住地址', trigger: 'blur'},
        ],
      },
      editPwdDialogVisible:false,
      pwdForm:{
        oldPass:'',
        newPass:'',
        checkPass:''
      },
      pwdFormRules: {
        oldPass: [
          {required: true, message: '请输入旧密码', trigger: 'blur'},
        ],
        newPass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
      }
    }
  },
  mounted() {
    this.initHrInfo();
  },
  methods:{
    onSuccess(){
      this.initHrInfo();
    },
    showUpdatePwdView(){
      this.editPwdDialogVisible=true;
    },
    submitFormOfUpdatePwd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 修改密码
          this.pwdForm.hrId=this.hr.id;
          this.putRequest("/system/hr/updatePwd",this.pwdForm).then(resp=>{
            if(resp){
              // 退出重新登录
              this.getRequest("/logout");
              // 清除用户数据
              window.sessionStorage.removeItem("user");
              // 清空菜单数据
              this.$store.commit('initRoutes',[]);
              // 路径到登录页面
              this.$router.replace("/")
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetFormOfUpdatePwd(formName) {
      this.$refs[formName].resetFields();
      this.editPwdDialogVisible=false;
    },


    showUpdateUserInfoView(){
      this.editHrInfoDialogVisible=true;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.putRequest("/system/hr/info",this.hrEdit).then(resp=>{
            if(resp){
              this.initHrInfo();
              this.editHrInfoDialogVisible=false;
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    cancelDialog(){
      this.editHrInfoDialogVisible=false;
      this.hrEdit=Object.assign({},this.hr);
    },
    initHrInfo(){
      this.getRequest("/system/hr/info").then(resp=>{
        if(resp){
          this.hr=resp.data;
          // 这样写不可以，这样写是 hr和 hrEdit都指向 resp.data;当修改一个的时候另一个也会修改
          //this.hrEdit=resp.data;
          this.hrEdit=Object.assign({},this.hr);
          window.sessionStorage.setItem("user",JSON.stringify(resp.data))
          this.$store.commit("INIT_CURRENT_HR",resp.data)
        }
      })
    }
  }
}
</script>

<style scoped>

</style>